/**
 * 仓库层
 */
package com.xiaomuwu.android.logic

import androidx.lifecycle.liveData
import com.xiaomuwu.android.logic.dao.XiaoMuWuDatabase
import com.xiaomuwu.android.logic.model.*
import kotlinx.coroutines.Dispatchers

object Repository {

    fun checkLogin(forCheckLogin: ForCheckLogin)=liveData(Dispatchers.IO) {
        val loginResult=XiaoMuWuDatabase.checkLogin(forCheckLogin)
        val result=try{
            Result.success(loginResult)
        }catch (e:Exception){
            Result.failure<String>(e)
        }
        emit(result)
    }

    fun registerUser(forOperateUser: ForOperateUser)=liveData(Dispatchers.IO) {
        val registerResult=XiaoMuWuDatabase.registerUser(forOperateUser)
        val result=try {
            Result.success(registerResult)
        }catch (e:Exception){
            Result.failure<String>(e)
        }
        emit(result)
    }

    fun modigyUser(forOperateUser: ForOperateUser)=
        XiaoMuWuDatabase.modigyUser(forOperateUser)

    fun getPersonal(userID: String?)=liveData(Dispatchers.IO){
        val personalData=XiaoMuWuDatabase.getPersonal(userID)
        val result=try {
            Result.success(personalData)
        }catch (e:Exception){
            Result.failure<PersonalData>(e)
        }
        emit(result)
    }

    fun getRes(forRes: ForRes)=liveData(Dispatchers.IO){
        val resData=XiaoMuWuDatabase.getRes(forRes)
        val result=try {
            Result.success(resData)
        }catch (e:Exception){
            Result.failure<List<Res>>(e)
        }
        emit(result)
    }

    fun getUser(forUser:ForUser)=liveData(Dispatchers.IO){
        val userData=XiaoMuWuDatabase.getUser(forUser)
        val result=try{
            Result.success(userData)
        }catch (e:Exception){
            Result.failure<List<User>>(e)
        }
        emit(result)
    }

    fun getReleRes(forReleRes: ForReleRes)=liveData(Dispatchers.IO){
        val releRes=XiaoMuWuDatabase.getReleRes(forReleRes)
        val result=try{
            Result.success(releRes)
        }catch (e:Exception){
            Result.failure<List<Res>>(e)
        }
        emit(result)
    }

    fun getReleUser(forReleUser: ForReleUser)=liveData(Dispatchers.IO){
        val releUser=XiaoMuWuDatabase.getReleUser(forReleUser)
        val result=try{
            Result.success(releUser)
        }catch (e:Exception){
            Result.failure<List<User>>(e)
        }
        emit(result)
    }

    fun getResDetail(forResDetail: ForResDetail)= liveData(Dispatchers.IO){
        val resDetail=XiaoMuWuDatabase.getResDetail(forResDetail)
        val result=try {
            Result.success(resDetail)
        }catch (e:Exception){
            Result.failure<ResDetail>(e)
        }
        emit(result)
    }

    fun getUserDetail(forUserDetail: ForUserDetail)=liveData(Dispatchers.IO){
        val userDetail=XiaoMuWuDatabase.getUserDetail(forUserDetail)
        val result=try {
            Result.success(userDetail)
        }catch (e:Exception){
            Result.failure<UserDetail>(e)
        }
        emit(result)
    }

    fun likeRes(forLike: ForLike)=
        XiaoMuWuDatabase.likeRes(forLike)

    fun downloadRes(forDownLoad: ForDownLoad)=
        XiaoMuWuDatabase.downloadRes(forDownLoad)

    fun followUser(forFollow: ForFollow)=
        XiaoMuWuDatabase.followUser(forFollow)

    fun uploadRes(forUpload: ForUpload)=
        XiaoMuWuDatabase.uploadRes(forUpload)

    fun modifyRes(forModify: ForModify)=
        XiaoMuWuDatabase.modigyRes(forModify)

    fun deleteRes(resID:Long)=
        XiaoMuWuDatabase.deleteRes(resID)

}